package com.beardedcoder.lowPower.activities;

import java.util.ArrayList;
import java.util.List;

import com.beardedcoder.lowPower.R;
import com.beardedcoder.lowPower.model.HomeScreenCellData;
import com.beardedcoder.lowPower.storage.StorageHomeScreen;
import com.beardedcoder.lowPower.views.HomeScreenView;
import com.beardedcoder.utils.power.AirplaneFeatures;
import com.beardedcoder.utils.power.FeaturesWifi;
import com.beardedcoder.utils.ui.ActionBar;
import com.beardedcoder.utils.ui.ActionBar.ActionImage;
import com.beardedcoder.utils.ui.ActionBar.ActionText;
import com.beardedcoder.utils.ui.MovableAdapter;
import com.beardedcoder.utils.ui.MovableAdapter.ListenerDataAltered;
import com.beardedcoder.utils.ui.view.MovableListView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

public class ActivityHomeScreen extends Activity {

	private ActionBar actionbar;
	private MovableListView list;
	private CustomAdapter customAdapter;
	private StorageHomeScreen homeScreenDataStorage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		configureVariables();
		configureState(savedInstanceState);
		configureViews();
	}

	/**
	 * onPause() is called when the activity wants to go out of view. 
	 */
	@Override
	protected void onPause() {
		homeScreenDataStorage.save(this);
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		refreshDisplay();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	private void refreshDisplay() {
		list.setDropListener(customAdapter);
		list.setDragListener(customAdapter);
	}

	private void configureViews() {
		setContentView(R.layout.home_screen);
		
		configureActionbar();
		
		list = (MovableListView) findViewById(android.R.id.list);
		list.setAdapter(customAdapter);
	}

	private void configureActionbar() {
		actionbar = (ActionBar) findViewById(R.id.actionbar);
		
		ActionImage settings = new ActionBar.ActionImage(this) {
			public void actionClicked() {
				Intent launchSettingsActivity = new Intent(ActivityHomeScreen.this, SettingsActivity.class);
				startActivity(launchSettingsActivity);
			}
		};
		settings.setBackgroundResource(R.drawable.ic_action_setting);
		actionbar.addAction(settings, ActionBar.CONTAINER_RIGHT);
		
		ActionImage close = new ActionBar.ActionImage(this) {
			public void actionClicked() {
				finish();
				// TODO: make new settings screen.
			}
		};
		close.setBackgroundResource(R.drawable.ic_action_close);
		actionbar.addAction(close, ActionBar.CONTAINER_RIGHT);
		
		
    	ActionText title = new ActionBar.ActionText(this) {
			public void actionClicked() { }
		};
		title.setText("Main Screen");
		title.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		title.setTextColour(getResources().getColor(android.R.color.black));
		actionbar.addAction(title, ActionBar.CONTAINER_TITLE);
	}

	private void configureState(Bundle savedInstanceState) {
	}

	private void configureVariables() {
		homeScreenDataStorage = new StorageHomeScreen(this);
		List<Object> dataItems = new ArrayList<Object>();
		dataItems.addAll(homeScreenDataStorage.getOrder());
		customAdapter = new CustomAdapter(dataItems);
		
		// register a listener to pick up changes to the order of the data items. 
		customAdapter.setListenerDataAltered(new ListenerDataAltered() {
			
			@Override
			public void newOrdering(List<Object> data) {
				ArrayList<HomeScreenCellData> items = new ArrayList<HomeScreenCellData>();
				for (Object dataItemSingle : data) {
					items.add((HomeScreenCellData) dataItemSingle);
				}
				homeScreenDataStorage.setOrder(items);
			}
		});
	}

	private class CustomAdapter extends MovableAdapter {

		public CustomAdapter(List<Object> dataItems) {
			super(dataItems);
		}

		@Override
		public View getView(int index, View convertView, ViewGroup arg2) {
			HomeScreenView view = convertView == null ? new HomeScreenView(ActivityHomeScreen.this) : (HomeScreenView) convertView;
			HomeScreenCellData info = (HomeScreenCellData) getItem(index);
			view.setData(info);
			
			if (info.description.equals(StorageHomeScreen.DESCRIPTION_AIRPLANE)) {
				// this shouldn't get a new one all the time, this is just a hack
				// this should be retrieved from the service, but as these aren't in the
				// service yet, and this activity hasn't bound to the service
				// then we are just creating it on the fly
				view.setPowerFeature(new AirplaneFeatures(ActivityHomeScreen.this));
				
			} else if (info.description.equals(StorageHomeScreen.DESCRIPTION_WIFI)) {
				view.setPowerFeature(new FeaturesWifi(ActivityHomeScreen.this));
				
			} else {
				view.setPowerFeature(null);
			}
			
			view.setVisibility(index == viewInMovement ? View.INVISIBLE : View.VISIBLE);
			return view;
		}
	}
}
